﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KATS_Client.UI
{
    /// <summary>Holds lists of objects found at a location</summary>
    public class DirectoryData
    {
        /// <summary>The HDDs at the location</summary>
        public string[] HDDS = new string[0];
        /// <summary>The directories at the location</summary>
        public string[] DIRECTORIES = new string[0];
        /// <summary>The files at the location</summary>
        public string[] FILES = new string[0];
        /// <summary>The location, where these files where found</summary>
        public Client.FileSystemObject Directory = null;

        public DirectoryData(Client.FileSystemObject fso)
        {
            this.Directory = fso;
        }


        /// <summary>Get data from the systems</summary>
        public void GetData()
        {
            if (Directory.IsMyComputer)
            {
                this.HDDS = Client.FileActions.HDDs(Directory.IsRemote);
            }
            else
            {
                this.DIRECTORIES = Client.FileActions.Directories(this.Directory);
                if (DIRECTORIES == null)
                    DIRECTORIES = new string[0];
                this.FILES = Client.FileActions.FilesAndSizes(this.Directory);
                if (FILES == null)
                    FILES = new string[0];
            }
        }


        /// <summary>
        /// The list view items generated by the "CreateListViewItems()" function
        /// </summary>
        public List<ListViewItem> ListViewItems = null;



        /// <summary>Create list view items from the server data</summary>
        public void CreateListViewItems()
        {
            if (Directory.IsMyComputer)
            {
                ListViewItems = new List<ListViewItem>(HDDS.Length);

                foreach (string hdd in HDDS)
                {
                    Client.FileSystemObject data = new KATS_Client.Client.FileSystemObject(hdd, Directory.IsRemote, KATS_Client.Client.FileSystemObjectTypes.Directory);
                    ListViewItem i = MakeItem(hdd, hdd, data, "HDD", "HDD_ICON");
                    ListViewItems.Add(i);
                }
            }
            else
            {
                ListViewItems = new List<ListViewItem>(DIRECTORIES.Length + FILES.Length);

                // create the directories
                foreach (string d in DIRECTORIES)
                {
                    Client.FileSystemObject data = new KATS_Client.Client.FileSystemObject(d, Directory.IsRemote, KATS_Client.Client.FileSystemObjectTypes.Directory);

                    string directory2 = d.Replace(this.Directory.Location, "");
                    directory2 = directory2.TrimStart(new char[] { '\\' });
                    ListViewItem i = MakeItem(directory2, directory2, data, "Directory", "Directory");
                    ListViewItems.Add(i);
                }
                // add the files
                foreach (string fileData in FILES)
                {
                    string[] bits = fileData.Split(new char[] { '?' });
                    string file = bits[0];
                    long bytes = long.Parse(bits[1]);
                    string size = Client.FileActions.SizeString(bytes);
                    Client.FileSystemObject data = new KATS_Client.Client.FileSystemObject(file, Directory.IsRemote, KATS_Client.Client.FileSystemObjectTypes.File);
                    string file2 = file.Replace(this.Directory.Location, "");
                    file2 = file2.TrimStart(new char[] { '\\' });
                    ListViewItem i = MakeItem(file2, size, data, "File", UI.Tab.AddExtension(file));
                    ListViewItems.Add(i);
                }
            }
        }

        /// <summary>
        /// Make a list view item using the data supplied
        /// </summary>
        /// <param name="Text">The text</param>
        /// <param name="ToolTip">The tool tip</param>
        /// <param name="FileData">The tag data</param>
        /// <param name="Type">The type of object</param>
        /// <param name="ImageListKey">The image to display</param>
        /// <returns></returns>
        public static ListViewItem MakeItem(string Text, string ToolTip, Client.FileSystemObject FileData, string Type, string ImageListKey)
        {
            ListViewItem item = new ListViewItem();
            item.Text = Text;
            item.ImageKey = ImageListKey;
            item.Tag = FileData;
            item.ToolTipText = ToolTip;


            ListViewItem.ListViewSubItem[] subItems =
                new ListViewItem.ListViewSubItem[]
                {
                    new ListViewItem.ListViewSubItem(item, Type)
                };
            item.SubItems.AddRange(subItems);

            return item;
        }
    }
}
